import axios from 'axios'
import { toast } from 'vue3-toastify'

const request = axios.create({
  baseURL: '/chunleibei/api'
})

request.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    toast(error.response.data.message ?? error.message ?? '系统异常')
    return Promise.reject(error)
  }
)

export const userApply = (user) => {
  return request.post('user/apply', user)
}

export const getUploadCredentials = () => {
  return request.get('/upload/credentials')
}

export const updatePersonalUrl = (id, personalUrl) => {
  return request.post(`user/${id}/personal`, {
    personalUrl
  })
}

export const updateRaceUrl = (id, raceUrl) => {
  return request.post(`user/${id}/race`, {
    raceUrl
  })
}

export const getRandomUserList = () => {
  return request.get(`user/random`)
}

export const getStats = () => {
  return request.get(`user/stats`)
}

export const getWechatConfig = () => {
  return request.get(`/wechat/config`, {
    params: {
      url: window.location.href
    }
  })
}
